/*
 * @Author: sunzhijun 
 * @Date: 2019-03-28 15:21:41 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2020-04-12 21:22:33
 */
/**
 * showNewIncreased 控制新增组件显示隐藏
 * @nodeType 调用的节点 'center' 合同处理中心， 'sign' 合同签订
 * **/


import React, { Component, Fragment } from 'react';
import { high, base, getUrlParam, toast, promptBox, cacheTools, pageTo} from 'nc-lightapp-front';
import {hrAjax as ajax} from 'src/hrpub/common/utils/utils';
import NewIncreased from '../../../../../public/components/NewIncreased';
import FormCard from '../FormCard';
// import { jumpToNode } from '../../../../../../hrpub/common/utils/utils';

const protocolAppCode = '60110501';

class SignModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            setModalVisible: false,
            appCode: props.appCode,
            nodeType: props.nodeType || '',
            isVisibleFormCard: props.isVisibleFormCard,
            rowsListData: [],  //新增查询所选数据 
            doneFlag: false // 完成
        }

        this.closeModal = this.closeModal.bind(this);
        this.prevHandleData = this.prevHandleData.bind(this);
        this.nextDetailData = this.nextDetailData.bind(this);
        this.doneSetting = this.doneSetting.bind(this);
        this.setRollBackFlag = this.setRollBackFlag.bind(this);
        this.cancelOccupy = this.cancelOccupy.bind(this);
    }

    componentDidMount() {

    }
    /**
     * @param {*} {data, flag, type, isEffect}
     * @param data {Array} 主键数据
     * @param flag {Boolean} 是否选择了协议 
     * @param type {String} 决定跳转的节点类型 3:合同已生效协议未签订
     * @param isSignEffect {Boolean} 合同签订是否已生效
     * @param isProtocolEffect {Boolean} 协议签订是否已生效
     * @memberof closeModal 关闭Modal弹窗
     */
    closeModal({data, jobpks, flag, type, isSignEffect, isProtocolEffect} = {}){
        let { nodeType } = this.state;
        if(!!flag){ //已选择协议
            this.openProtocolModal(data,jobpks,flag);
        }else{
            let url = {
                contractNode : '/nccloud/resources/hrcm/contractmgt/sign/main/index.html', //合同
                protocolNode : '/nccloud/resources/hrcm/protocolmgt/sign/main/index.html' //协议
            }
            if(!!nodeType && nodeType == 'center'){ //合同签订时点击取消跳转到合同签订页面或者协议签订页面
                this.fromCenterJumpToNode(data,isSignEffect,isProtocolEffect,url,flag,type);
            }else if(nodeType == 'sign' && type == '2'){
                this.fromSignJumpToNode(data,url);
            }else{ //取消新增弹窗
                this.setModalInvisible(data);   
            }
        }
    }
    //打开协议弹窗
    openProtocolModal(data,jobpks,flag){
        this.setState({
            setModalVisible: false
        },()=>{
            this.setState({ //调用协议新增弹窗
                setModalVisible: true,
                agFlag: flag,
                appCode : protocolAppCode
            },()=>{
                this.newIncreased.getSearchData([],jobpks, 'psnList');
            })
            this.props.fetchTableData && this.props.fetchTableData({key: data,type: '1'});
        });
    }

    //关闭弹窗
    setModalInvisible(data){
        this.setState({
            setModalVisible: false, //关闭弹窗
            agFlag: false,
            appCode: ''
        },()=>{
            if(!!data){
                this.props.fetchTableData && this.props.fetchTableData({key: data,type: '1'}); //1区分与缓存取得的数据
            }
            this.props.closeFormCardModal && this.props.closeFormCardModal(data);
        });
        if (!this.state.doneFlag) this.cancelOccupy();
    }

    //对合同处理中心返回的数据进行跳转
    fromCenterJumpToNode(data, isSignEffect, isProtocolEffect, url, flag, type){
        let pks = cacheTools.get('pkPsndocSubs') || data;
        if(type != '3'){
            if(isSignEffect && !isProtocolEffect){ //签订已生效 协议未生效
                this.props.jumpToNode && this.props.jumpToNode(data,url.protocolNode,true);
            }else if(!isSignEffect && isProtocolEffect){ //签订未生效 协议已生效
                this.props.jumpToNode && this.props.jumpToNode(pks,url.contractNode,true);
            }else if( !isSignEffect && !isProtocolEffect ){ //签订未生效 协议未生效
                this.props.jumpToNode && this.props.jumpToNode(pks,url.contractNode,true);
                if(type == 2){ //协议
                    this.props.jumpToNode && this.props.jumpToNode(data,url.protocolNode,true);
                }
            }
        }
        this.props.closeFormCardModal && this.props.closeFormCardModal();
    }

    //对合同签订返回的数据进行跳转
    fromSignJumpToNode(data,url){
        let protocolNode = url && url.protocolNode;
        this.props.storageOrgData && this.props.storageOrgData();
        pageTo.openTo(protocolNode,{
            appcode:'60110501',
            pagecode:'60110501p',
        })
        // jumpToNode(protocolNode);
        this.setModalInvisible();
    }

    //上一步
    prevHandleData(){
        this.cancelOccupy();
    }

    //下一步
    nextDetailData(selectData,agreementType){ 
        // window.localStorage.setItem("listData",JSON.stringify(selectData));
        // this.formCard.getFormCardData(selectData,agreementType);
        let pk_psnjob = [];
        selectData && selectData.map((v,i)=>{
            pk_psnjob.push(v.data.values.pk_psnjob.value);
        });
        this.setState({
            rowsListData: pk_psnjob
        },()=>{
            this.formCard.getFormCardData(selectData, agreementType);
        })
    }

    //点击完成保存
    doneSetting(agFlag,agreementType){
        this.setState({
            doneFlag: true
        }, () => {
            if(!agFlag){
                //调用合同保存
                this.formCard.doneDetailData();
            }else{
                //调用协议保存
                let { rowsListData } = this.state;
                this.formCard.doneAgreementData(agreementType, rowsListData);
                // let listDataStorage = JSON.parse(window.localStorage.getItem('listData'));
                // this.formCard.doneAgreementData(agreementType,listDataStorage);
            }
        })
    }

    //设置setRollBackFlag
    setRollBackFlag(rollBackFlag){
        this.setState({
            rollBackFlag: rollBackFlag
        })
    }

    //取消下一步人员数据被占用
    cancelOccupy(){
        let {pkOrg} = this.props;
        let { rollBackFlag, pk_org, agFlag} = this.state;
        let formCardId = this.formCard && this.formCard.getFormCardId();
        if( formCardId && rollBackFlag ){
            let occupyData = this.props.form.getAllFormValue(formCardId);
            let occupyValues = occupyData.rows[0].values;
            let occupyCode = occupyData && agFlag ? occupyValues.contcode.value : occupyValues.contractnum.value;
            let occupyCodeArr = new Array(occupyCode);
            ajax ({
                url: '/nccloud/hrcm/common/CodeRollBackAction.do',
                data: {
                    codeList: occupyCodeArr,
                    pk_org: pkOrg
                },
                success: res =>{
                    this.setState({
                        doneFlag: false
                    })
                }
            })
        }
    }

    //控制新增组件显示隐藏
    showNewIncreased(isShow,jobpks,agFlag){
        this.setState({
            setModalVisible : isShow
        })
    }

    //调用FormCard中的方法
    getFormCardData(checkedRows){
        this.formCard.getFormCardData(checkedRows);
    }
    doneHandle(){
        this.formCard.doneHandle();
    }

    render () {
        const { pkOrg } = this.props;
        let { isVisibleFormCard, setModalVisible, agFlag, appCode } = this.state;
        return (
            <React.Fragment>
                {
                    isVisibleFormCard ? 
                        <FormCard
                        {...this.props}
                        agFlag = {agFlag}
                        appCode = {appCode}
                        ref={node => {
                            this.formCard = node
                        }}
                        setRollBackFlag = { this.setRollBackFlag }
                        closeFormCardModal = {this.closeModal}
                    ></FormCard> : ''
                }
                {
                    setModalVisible ? 
                    <NewIncreased 
                        {...this.props}
                        ref = { node => {
                            this.newIncreased = node
                        }}
                        setModalVisible = {setModalVisible}
                        agFlag = {agFlag}
                        appCode = {appCode}
                        onClose = {this.closeModal}                 //关闭弹窗回调
                        prevHandle={this.prevHandleData}            //上一步操作方法
                        nextHandle={this.nextDetailData}            //下一步操作方法
                        doneHandle={this.doneSetting}    //完成操作方法
                    >     
                        {/* 自定义区域 */}
                        <FormCard
                            {...this.props}
                            agFlag = {agFlag}
                            appCode = {appCode}
                            ref={node => {
                                this.formCard = node
                            }}
                            closeFormCardModal = {this.closeModal}
                            setRollBackFlag = { this.setRollBackFlag }
                        ></FormCard>
                    </NewIncreased> : ''
                }
            </React.Fragment>
        )
    }
}
export default SignModal;